package com.framework.service.impl;

import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.framework.dao.BenefitInfoDao;
import com.framework.entity.sys.SysAgent;
import com.framework.entity.tran.TranBenefitInfo;
import com.framework.entity.tran.TranTerBenefitInfo;
import com.framework.entity.tran.TranTransactionInfo;
import com.framework.page.PageUtils;
import com.framework.service.AgentService;
import com.framework.service.BenefitInfoService;
import com.framework.utils.string.DateStrUtils;
import com.framework.utils.string.StringUtil;
import com.framework.view.TranAmountView;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Transactional(rollbackFor =Exception.class)
@Service("benefitInfoService")
public class BenefitInfoServiceImpl extends ServiceImpl<BenefitInfoDao, TranBenefitInfo> implements BenefitInfoService {

	@Autowired
	private AgentService agentService;

	@Override
	public List<TranAmountView> separatSelfDayList(Integer terminalType, String dayTime, Long agentId, Long deptId, Integer types) {
		return baseMapper.separatSelfDayList(terminalType,dayTime,agentId,deptId,types);
	}

	@Override
	public List<Map<String, Object>> queryLeShuaDayList(Integer terminalType, String dayTime, Long agentId, Long deptId, Integer types) {
		return baseMapper.queryLeShuaDayList(terminalType, dayTime, agentId, deptId, types);
	}

	@Override
	public Map<String, Object> queryTranChartMap(String token) {
		Map<String,Object> map = new HashedMap();
		SysAgent agent = agentService.queryAgentByToken(token);
		Long agentId = agent.getAgentId();
		Long deptId = agent.getDeptId();
		//获取本月时间
		String thisMonth = StringUtil.getLast12Months(0);
		//获取前2个月的时间
		String lastMonth = StringUtil.getLast12Months(1);
		String lastTwoMonth = StringUtil.getLast12Months(2);
		//饼状图数据
		List<Map<String,Object>> thisTranSelf = baseMapper.queryTranPancake(agentId,deptId,thisMonth,1);
		List<Map<String,Object>> thisTranTeam = baseMapper.queryTranPancake(agentId,deptId,thisMonth,2);
		map.put("thisTranSelf",thisTranSelf);
		map.put("thisTranTeam",thisTranTeam);
		//柱状图数据
		String[] lastMonthArray = lastMonth.split("-");
		String[] lastTwoMonthArray = lastTwoMonth.split("-");
		List<Map<String,Object>> lastTranColumnar = new ArrayList<>();
		List<Map<String,Object>> lastTwoTranColumnar = new ArrayList<>();
		for (int i = 1; i <= 4; i++) {
			Map<String,Object> lastTimeMap = DateStrUtils.getScopeForweeks(Integer.valueOf(lastMonthArray[0]),Integer.valueOf(lastMonthArray[1]),i);
			Map<String,Object> lastTwoTimeMap = DateStrUtils.getScopeForweeks(Integer.valueOf(lastTwoMonthArray[0]),Integer.valueOf(lastTwoMonthArray[1]),i);
			String lastBeginDate = (String)lastTimeMap.get("beginDate");
			String lastEndDate = (String)lastTimeMap.get("endDate");
			String lastTwoBeginDate = (String)lastTwoTimeMap.get("beginDate");
			String lastTwoEndDate = (String)lastTwoTimeMap.get("endDate");
			//根据年月获取最后一天时间
			if(i == 4){
				 lastEndDate = DateStrUtils.getLastDayOfMonth1(Integer.valueOf(lastMonthArray[0]),Integer.valueOf(lastMonthArray[1]));
				 lastTwoEndDate = DateStrUtils.getLastDayOfMonth1(Integer.valueOf(lastTwoMonthArray[0]),Integer.valueOf(lastTwoMonthArray[1]));

			}
			Map<String,Object> lastTran = new HashedMap();
			Map<String,Object> lastTwo = new HashedMap();
			lastTran.put("lastTran",baseMapper.queryTranColumnar(agentId,lastBeginDate,lastEndDate));
			lastTwo.put("lastTwo",baseMapper.queryTranColumnar(agentId,lastTwoBeginDate,lastTwoEndDate));
			lastTranColumnar.add(lastTran);
			lastTwoTranColumnar.add(lastTwo);
		}
		map.put("lastTranColumnar",lastTranColumnar);
		map.put("lastTwoTranColumnar",lastTwoTranColumnar);
		return map;
	}

	@Override
	public PageUtils queryDetailByBenefitId(Map<String, Object> params) {
		Integer pageSize=Integer.valueOf(params.get("limit").toString());
		Integer currPage=Integer.valueOf(params.get("page").toString());
		params.put("start",(currPage-1)*pageSize);
		params.put("limit",pageSize);
		List<Map<String,Object>> list=this.baseMapper.queryDetailByBenefitId(params);
		Integer totalCount=this.baseMapper.queryCountByBenefitId(params);
		return new PageUtils(list,totalCount,pageSize,currPage);
	}

	@Override
	public void addSelfBenefitInfo(TranTransactionInfo tranTransactionInfo, BigDecimal benefitAmount, SysAgent agent, String no) {
		//检查是否有个人每日分润，同一机器类别的详细信息,有则修改,无则添加
		TranBenefitInfo t=new TranBenefitInfo();
		t.setBenefitId(no);
		t.setTerminalType(tranTransactionInfo.getTerminalType());
		t.setIsVip(tranTransactionInfo.getIsVip());
		TranBenefitInfo benefitInfo = baseMapper.selectOne(t);
		//添加
		if(benefitInfo==null){
			benefitInfo=new TranBenefitInfo();
			benefitInfo.setTerminalType(tranTransactionInfo.getTerminalType());
			benefitInfo.setBenefitId(no);
			benefitInfo.setIsVip(tranTransactionInfo.getIsVip());
			benefitInfo.setSelfInfoTran(tranTransactionInfo.getAmount());
			benefitInfo.setSelfInfoAmount(benefitAmount);
			benefitInfo.setTeamInfoAmount(new BigDecimal("0.00"));
			benefitInfo.setTeamInfoTran(new BigDecimal("0.00"));
			benefitInfo.setAllInfoAmount(benefitAmount);
			benefitInfo.setAllInfoTran(tranTransactionInfo.getAmount());
			baseMapper.insert(benefitInfo);

		}else {//修改
			BigDecimal selfInfoTran=benefitInfo.getSelfInfoTran();
			BigDecimal selfInfoAmount=benefitInfo.getSelfInfoAmount();
			BigDecimal allInfoTran=benefitInfo.getAllInfoTran();
			BigDecimal allInfoAmount=benefitInfo.getAllInfoAmount();
			selfInfoTran=selfInfoTran.add(tranTransactionInfo.getAmount());
			selfInfoAmount=selfInfoAmount.add(benefitAmount);
			allInfoTran=allInfoTran.add(tranTransactionInfo.getAmount());
			allInfoAmount=allInfoAmount.add(benefitAmount);
			benefitInfo.setAllInfoTran(allInfoTran);
			benefitInfo.setAllInfoAmount(allInfoAmount);
			benefitInfo.setSelfInfoAmount(selfInfoAmount);
			benefitInfo.setSelfInfoTran(selfInfoTran);
			baseMapper.updateAllColumnById(benefitInfo);
		}

	}

	@Override
	public void updateTeamTranInfo(TranTerBenefitInfo tranTerBenefitInfo, BigDecimal teambenefit, SysAgent sysAgent, String benefitId) {
		//检查是否有个人每日分润，同一机器类别的详细信息,
		TranBenefitInfo t=new TranBenefitInfo();
		t.setBenefitId(benefitId);
		t.setTerminalType(tranTerBenefitInfo.getTerminalType());
		t.setIsVip(tranTerBenefitInfo.getIsVip());
		TranBenefitInfo benefitInfo = baseMapper.selectOne(t);
		if(benefitInfo==null){
			benefitInfo=new TranBenefitInfo();
			benefitInfo.setTerminalType(tranTerBenefitInfo.getTerminalType());
			benefitInfo.setBenefitId(benefitId);
			benefitInfo.setIsVip(tranTerBenefitInfo.getIsVip());
			benefitInfo.setSelfInfoTran(new BigDecimal("0.00"));
			benefitInfo.setSelfInfoAmount(new BigDecimal("0.00"));
			benefitInfo.setAllInfoAmount(teambenefit);
			benefitInfo.setAllInfoTran(tranTerBenefitInfo.getTranAmount());
			benefitInfo.setTeamInfoTran(tranTerBenefitInfo.getTranAmount());
			benefitInfo.setTeamInfoAmount(teambenefit);
			baseMapper.insert(benefitInfo);
		}else {
			//如果为空
			if (benefitInfo.getTeamInfoAmount() == null) {
				benefitInfo.setTeamInfoAmount(new BigDecimal("0.00"));
			}
			if (benefitInfo.getTeamInfoTran() == null) {
				benefitInfo.setTeamInfoTran(new BigDecimal("0.00"));
			}
			BigDecimal teamTran = benefitInfo.getTeamInfoTran().add(tranTerBenefitInfo.getTranAmount());
			BigDecimal teamAmount = benefitInfo.getTeamInfoAmount().add(teambenefit);
			BigDecimal alltran = benefitInfo.getAllInfoTran().add(tranTerBenefitInfo.getTranAmount());
			BigDecimal allamount = benefitInfo.getAllInfoAmount().add(teambenefit);
			benefitInfo.setTeamInfoTran(teamTran);
			benefitInfo.setTeamInfoAmount(teamAmount);
			benefitInfo.setAllInfoAmount(allamount);
			benefitInfo.setAllInfoTran(alltran);
			baseMapper.updateAllColumnById(benefitInfo);

		}
	}

	@Override
	public void updateTeamBenefitInfo(TranTerBenefitInfo info, BigDecimal benefitAmount, SysAgent sysAgent, String benefitId) {
		//检查是否有个人每日分润，同一机器类别的详细信息,
		TranBenefitInfo t=new TranBenefitInfo();
		t.setBenefitId(benefitId);
		t.setTerminalType(info.getTerminalType());
		t.setIsVip(info.getIsVip());
		TranBenefitInfo benefitInfo = baseMapper.selectOne(t);
		if(benefitInfo==null){
			System.out.println("--日分润id"+benefitId+"-类别：-"+info.getTerminalType()+"--没有");
		}else {
			//如果为空
			if (benefitInfo.getTeamInfoAmount() == null) {
				benefitInfo.setTeamInfoAmount(new BigDecimal("0.00"));
			}
			if (benefitInfo.getTeamInfoTran() == null) {
				benefitInfo.setTeamInfoTran(new BigDecimal("0.00"));
			}
			BigDecimal teamAmount = benefitInfo.getTeamInfoAmount().add(benefitAmount);
			BigDecimal allAmount = benefitInfo.getAllInfoAmount().add(benefitAmount);
			benefitInfo.setTeamInfoAmount(teamAmount);
			benefitInfo.setAllInfoAmount(allAmount);
			baseMapper.updateAllColumnById(benefitInfo);
		}
	}

	@Override
	public void addBenefitInfo(TranTransactionInfo info, BigDecimal pidBenefit, SysAgent pAgent, String benefitId) {
		//检查是否有个人每日分润，同一机器类别的详细信息,
		TranBenefitInfo t=new TranBenefitInfo();
		t.setBenefitId(benefitId);
		t.setTerminalType(info.getTerminalType());
		t.setIsVip(info.getIsVip());
		TranBenefitInfo benefitInfo = baseMapper.selectOne(t);
		if(benefitInfo==null){
			benefitInfo=new TranBenefitInfo();
			benefitInfo.setTerminalType(info.getTerminalType());
			benefitInfo.setBenefitId(benefitId);
			benefitInfo.setIsVip(info.getIsVip());
			benefitInfo.setSelfInfoTran(new BigDecimal("0.00"));
			benefitInfo.setSelfInfoAmount(new BigDecimal("0.00"));
			benefitInfo.setAllInfoAmount(pidBenefit);
			benefitInfo.setAllInfoTran(info.getAmount());
			benefitInfo.setTeamInfoTran(info.getAmount());
			benefitInfo.setTeamInfoAmount(pidBenefit);
			baseMapper.insert(benefitInfo);
		}else {
			//如果为空
			if (benefitInfo.getTeamInfoAmount() == null) {
				benefitInfo.setTeamInfoAmount(new BigDecimal("0.00"));
			}
			if (benefitInfo.getTeamInfoTran() == null) {
				benefitInfo.setTeamInfoTran(new BigDecimal("0.00"));
			}
			BigDecimal teamTran = benefitInfo.getTeamInfoTran().add(info.getAmount());
			BigDecimal teamAmount = benefitInfo.getTeamInfoAmount().add(pidBenefit);
			BigDecimal alltran = benefitInfo.getAllInfoTran().add(info.getAmount());
			BigDecimal allamount = benefitInfo.getAllInfoAmount().add(pidBenefit);
			benefitInfo.setTeamInfoTran(teamTran);
			benefitInfo.setTeamInfoAmount(teamAmount);
			benefitInfo.setAllInfoAmount(allamount);
			benefitInfo.setAllInfoTran(alltran);
			baseMapper.updateAllColumnById(benefitInfo);

		}
	}


}
